package com.dotools.privacy;


import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dotools.procommon.R;
import com.dotools.umlibrary.UMPostUtils;

/**
 * 好评弹窗
 */
public class PraiseDialog {
    private AlertDialog mDialog = null;
    private AlertDialog.Builder builder;
    private View view;

    public interface PraiseBtnClickListener {
        void onThumbUpClick();
        void onComplainClick();
        void onCloseClick();
        void onCheckedClick(boolean isChecked);
    }

    public PraiseDialog(final Context context, final PraiseBtnClickListener listener) {
        builder = new AlertDialog.Builder(context, R.style.Privacy_Dialog_Style);
        view = LayoutInflater.from(context).inflate(R.layout.praise_dialog, null, false);
        TextView ok = view.findViewById(R.id.praise_ok);
        TextView none = view.findViewById(R.id.praise_no);
        ImageView close = view.findViewById(R.id.praise_close);
        CheckBox understand = view.findViewById(R.id.understand_cb);
        understand.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				UMPostUtils.INSTANCE.onEvent(context, "thumbup_no_again_click");
                listener.onCheckedClick(isChecked);
            }
        });
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogDismiss();
                UMPostUtils.INSTANCE.onEvent(context, "thumbup_5stars_click");
                listener.onThumbUpClick();
                try {
                    Uri uri = Uri.parse("market://details?id=" + context.getPackageName());
                    Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
                    context.startActivity(goToMarket);
                } catch (Exception e) {
                    Toast.makeText(context, "未找到应用商店", Toast.LENGTH_SHORT).show();
                }
            }
        });
        none.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogDismiss();
                UMPostUtils.INSTANCE.onEvent(context, "thumbup_complain_click");
                listener.onComplainClick();
            }
        });
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogDismiss();
                listener.onCloseClick();
            }
        });
    }

    public void show() {
        mDialog = builder.setView(view).create();
        mDialog.setCanceledOnTouchOutside(false);//点击屏幕不消失
        // 设置参数
        WindowManager.LayoutParams params = mDialog.getWindow().getAttributes();
        // 宽度和高度
        params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.CENTER;
        mDialog.getWindow().setAttributes(params);
        mDialog.show();
        UMPostUtils.INSTANCE.onEvent(view.getContext(), "thumbup_pop_show");
    }

    public void dialogDismiss() {
        if (mDialog != null) {
            if (mDialog.isShowing()) {
                mDialog.dismiss();
                mDialog = null;
            }
        }
    }
}

